<?php


namespace Api\Controller;


/** 
 * 订阅操作  
 function:
 mine        我的关注 
 scribe 	 关注
 unscribe    去关
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

class ScribeController extends UserBaseController 
{

	public $uid;
	public $game_id;


	/** 
	 * 订阅游戏
	 * @author Legend. <xcx_legender@qq.com>
	 * @param $game_id 
	 * @return 
	 */
	
	public function scribe()
	{
		$this->check_param();
		$res = A('GameScribe', 'Event')->scribe($this->uid, $this->game_id);
		$res ? $this->success('订阅成功') : $this->error(self::$API_ERROR['HAS_SCRIBE'], '已经订阅');
	}
	 

	public function scribe_more()
	{
		$is = event('GameScribe')->count_mine(UID);
		if ($is)
		{
			return $this->error(self::$API_ERROR['HAS_SCRIBE'], '已经订阅');
		}
		$games = I('games', '');
		if (!$games)
		{
			$this->param_error();
		}
		$games = explode(',', $games);
		foreach ($games as $game_id) {
			$game_id = intval(trim($game_id) );
			$game_id > 0 && event('GameScribe')->scribe(UID, $game_id);
		}
		$this->success('订阅成功');
	}


	/** 
	 * 取消订阅
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	public function unscribe()
	{
		$this->check_param();
		$res = A('GameScribe', 'Event')->unscribe($this->uid, $this->game_id);
		$res ? $this->success('取消成功') : $this->error(self::$API_ERROR['HAS_NO_SCRIBE'], '未订阅不能取消');
	}

	/** 
	 * 参数检查
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	protected function check_param()
	{
		$this->uid = UID;
		$game_id = I('game_id', 0, 'intval');
		if (!$game_id)
		{
			$this->param_error();
		}

		$game = A('Game', 'Event')->info($game_id);

		if (!$game)
		{
			$this->error(self::$API_ERROR['GAME']);
		}

		$this->game_id = $game_id;
	}
		
	/**
	*获取用户关注的游戏列表
	*@author susy<susy.liang@ilongyuan.com.cn> 2015/5/14
	*/
	public function games(){

		$eventObj = A('Api/GameScribe', 'Event');

		$games = $eventObj->mine(UID, true);

		//$data = array('code'=>self::$API_ERROR['SUCCESS'], 'info'=>'请求成功', 'lists'=>null);
		if($games){
			$temp = array();
			foreach ($games as $key => $value) {
				uri_format($value['pic']);
				uri_format($value['cover']);
				array_push($temp, $value);
			}
			
		}
		$this->success('', $temp);
	}

}